#include <func.h>
#include"threadPool.h"
#include"head.h"
#include"taskQueue.h"
int makeWorker(threadPool_t *pThreadPool){
    for(int i =0;i < pThreadPool->threadNum;++i){
        pthread_create(pThreadPool->tidArr+1,NULL,threadFunc,(void*)pThreadPool);
    }
}

int OrderJudge(task_t * pTask){
    char buf[1024]={0};
    if(strncmp(pTask->buf,"cd ",3)==0){
        cd(pTask);
    }
    else if(strncmp(pTask->buf,"cd",2)==0){
        cd(pTask);
    }
    else if(strncmp(pTask->buf,"ls ",3)==0){
        ls(pTask);
    }
    else if(strncmp(pTask->buf,"ls",2)==0){
        ls(pTask);
    }
    else if(strncmp(pTask->buf,"puts ",5)==0){
       //cd(pTask);
       uploadFile(pTask);
    }
    else if(strncmp(pTask->buf,"gets ",5)==0){
        //puts("gets");
        downloadFile(pTask);
    }
    else if(strncmp(pTask->buf,"remove ",7)==0){
        //puts("remove");
        removeFile(pTask);
    }
    else if(strncmp(pTask->buf,"pwd",3)==0){
       printWorkingDir(pTask);
       //puts("pwd");
    }
    else if(strncmp(pTask->buf,"mkdir ",6)==0){
        myMkdir(pTask);
    }
    else if(strncmp(pTask->buf,"rmdir ",6)==0){
        myRmdir(pTask);
        //puts("rmdir");
    }
    else{
        send(pTask->netFd,"Command Error,please try again!",31,0);
        return -1;
    }
}
void *threadFunc(void *parg){
    threadPool_t *pThreadPool = (threadPool_t *)parg; //地址传递传入传出参数
    while (1){
        //int netFd;
        task_t *pTask;
        //bzero(pTask,sizeof(task_t));
        pthread_mutex_lock(&pThreadPool->mutex);
        // pthread_cleanup_push(unlock,&pThreadPool->mutex);
        //加个判断==1防止醒来立马又循环等待
        while (pThreadPool->taskQueue.size == 0){
            //puts("wait");
            pthread_cond_wait(&pThreadPool->cond,&pThreadPool->mutex);//解锁了等一个信号
            //puts("wait2");
        }
        puts("GetTask");//来信号了自动上锁
        pTask = pThreadPool->taskQueue.pFront;
        deQueue(&pThreadPool->taskQueue);
        pthread_mutex_unlock(&pThreadPool->mutex);
        printf("netFd = %d\n",pTask->netFd);
        handleEvent(pTask);
    }
}
int handleEvent(task_t * pTask){
    task_t myTask = *pTask;
    puts("welcome into handEvent!");
    int netFd = myTask.netFd;
    //mysqlInit(&myTask);
    //usrConnectRecord(mypTask);//记录日志信息cat /var/log/syslog查看日志信息
    //从这开始是密码验证需要的，将
    while(1){
        //char userName[20] = {0};
        //char password[20] = {0};
        int ret = recv(netFd, myTask.buf, sizeof(myTask.buf), 0);
        if(atoi(myTask.buf) == 0){
            ret = recv(netFd, myTask.userName, sizeof(myTask.userName), 0);
            ERROR_CHECK(ret,-1,"recv");
            ret = recv(netFd, myTask.password, sizeof(myTask.password), 0);
            ERROR_CHECK(ret,-1,"recv");
            //puts("1");
            mysqlInit(&myTask);
            ret = login(&myTask);
            //puts("2");
            if(ret == 0){
                send(netFd, "0", 2, 0);
                break;
            }else if(ret == -1){
                send(netFd, "2", 2, 0);
                //循环再次输入
            }
        }
        else if(atoi(myTask.buf) == 1){
            //mysqlInit(&myTask);
            ret = recv(netFd, myTask.userName, sizeof(myTask.userName), 0);
            ERROR_CHECK(ret,-1,"recv");
            ret = recv(netFd, myTask.password, sizeof(myTask.password), 0);
            ERROR_CHECK(ret,-1,"recv");
            //puts("1");
            mysqlInit(&myTask);
            ret = myRegister(&myTask);
            //puts("2");
            if(ret == 0){
                send(netFd, "1", 2, 0);
                break;
                //login(&myTask);
            }else if(ret == -1){
                send(netFd, "2", 2, 0);
                //循环再次输入
            }
        }
        else{
            send(netFd,"-2",3,0);
            puts("client send error num,now exit!");
            //printf("输入错误，请重新输入！\n");
            exit(-1);
        }
        
    }
    //usrConnectRecord(&myTask);
    while (1){
        bzero(myTask.buf,sizeof(myTask.buf));
        int ret = recv(netFd,myTask.buf,sizeof(myTask.buf),0);
        if(ret == 0){
            puts("对面客户端断开！");
            break;
        }
        OrderJudge(&myTask);
        //usrCommandRecord(&myTask);
    }
    close(netFd);
    return 0;
}
